<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>第四节课</title>
		<style type="text/css">
			/*去除原来样式*/
			
			* {
				padding: 0;
				margin: 0;
			}
			
			.container {
				width: 100vw;
				height: 100vh;
				display: flex;
				/*
				 * 将边框设置在中间
				 */
				align-items: center;
				justify-content: center;
			}
			
			canvas {
				width: 500px;
				height: 500px;
				border: 1px solid #333333;
			}
		</style>
	</head>

	<body>
		<div class="container">
			<canvas id="canvas" width="600" height="600"></canvas>
		</div>
	</body>

	<script>
		window.onload=function(){
			
		
		let oCanvas = document.getElementById("canvas");
		let pen = oCanvas.getContext("2d");
		//pen.lineWidth=0;
		//console.dir(Pen);
		let data=[
				/*{name:'北京', color:'yellow', value:0.3},
        {name:'上海', color:'red', value:0.2},
        {name:'广州', color:'green', value:0.1},
        {name:'深圳', color:'purple', value:0.15},
        {name:'天津', color:'blue', value:0.25}*/
       
      {name:'第一季度',value:100, color:'red'},
       {name:'第二季度',value:50, color:'green'},
       {name:'第三季度',value:85, color:'blue'},
       {name:'第四季度',value:150, color:'yellow'}
			]
		
		createSector(pen,300,300,100,data);
		
		function createSector(oPen,x,y,r,data){
			sum = 0;
			for(var i=0;i<data.length;i++){
				sum+=data[i].value;
			}
			for(var i=0;i<data.length;i++){
				data[i].percentage = ((data[i].value*1.0/sum)*360)
			}
			let startAngle=0;
			for(var i=0;i<data.length;i++){
				pen.beginPath();
				pen.moveTo(x,y);
				pen.arc(x,y,r,startAngle,startAngle+(Math.PI/180)*data[i].percentage,false);
				startAngle+=(Math.PI/180)*data[i].percentage;
				pen.fillStyle=data[i].color;
				pen.fill();
				pen.stroke();
				pen.closePath();
			}
			console.dir(data);
			
		}
		
		}
	</script>

</html>